<?php

class My_Form extends Zend_Form
{
    protected $formDecorators = array(
        'FormElements',
        'table' => array(
            'HtmlTag',
            array('tag' => 'table', 'border' => '0', 'cellpadding' => '5', 'cellspacing' => '5'),
	),
        'Form',
    );

    protected $elementDecorators = array(
        'ViewHelper',
        'Errors',
        array(array('data' => 'HtmlTag'), array('tag' => 'td')),
        array('label', array('tag' => 'td')),
        array(array('row' => 'HtmlTag'), array('tag' => 'tr')),
    );

    protected $buttonDecorators = array(
        'ViewHelper',
        array(array('data' => 'HtmlTag'), array('tag' => 'td', 'colspan' => '2')),
        array(array('row' => 'HtmlTag'), array('tag' => 'tr')),
    );

    protected $hiddenDecorators = array(
        'ViewHelper',
    );

    public function loadDefaultDecorators()
    {
        $this->setDecorators($this->formDecorators);
    }

    public function init()
    {
        $this->addPrefixPath('My_Form', 'My/Form')
            ->addElementPrefixPath('My_Validate', 'My/Validate/', Zend_Form_Element::VALIDATE);
    }


    public function setAllDecorators() {
        foreach ($this->getElements() as $element) {
            $elementType = $element->getType();
            switch ($elementType) {
                case 'Zend_Form_Element_Submit':
                case 'Zend_Form_Element_Button':
                case 'Zend_Form_Element_Textarea':
                    $element->setDecorators($this->buttonDecorators);
                break;
                case 'Zend_Form_Element_Hidden':
                    $element->setDecorators($this->hiddenDecorators);
                break;
                default:
                    $element->setDecorators($this->elementDecorators);
            }
        }
    }
}